package za.ac.wits.elen4010.capturetheflag;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

// TODO: Auto-generated Javadoc
/**
 * The Class ArenaMap.
 */
public class ArenaMap extends MapActivity implements LocationListener {	
	// Private Datamembers
	/** The _arena map view. */
	private MapView _arenaMapView = null;
	
	/** The _my map controller. */
	private MapController _myMapController = null;
	
	/** The _my flag drawable. */
	private Drawable _myFlagDrawable = null;
	
	/** The _my flag overlay. */
	private FlagOverlay _myFlagOverlay = null;
	
	/** The _arena overlays. */
	private List<Overlay> _arenaOverlays = null;
	/*private GeoPoint flag1Point = null;
	private GeoPoint flag2Point = null;*/
	/** The _map centre. */
	private GeoPoint _mapCentre = null;
	/*private OverlayItem flag1 = null;
	private OverlayItem flag2 = null;*/
	/** The _player location manager. */
	private LocationManager _playerLocationManager = null;
	
	/** The _my game environment. */
	private GameEnvironment _myGameEnvironment = null;
	
	/**
	 * Setup arena.
	 */
	private final void setupArena() {
		setContentView(R.layout.arenamapview);
		_arenaMapView = (MapView) findViewById(R.id.arenaview);
		_arenaMapView.setBuiltInZoomControls(true);
		_arenaMapView.setSatellite(true);
		_myMapController = _arenaMapView.getController();
		_mapCentre = new GeoPoint(-26191516,28027850);
		_myMapController.setCenter(_mapCentre);
		_myMapController.setZoom(18);
		_myGameEnvironment = new GameEnvironment(this);
		
		_myGameEnvironment.createGame(this.getResources().getString(R.string.user_gameId));
	}
	
	/**
	 * Populate arena.
	 */
	private final void populateArena() {
		// Create flags
		_myFlagDrawable = this.getResources().getDrawable(R.drawable.flag);
		
		
		/*flag1Point = new GeoPoint(-26191516,28027850);
		flag1 = new OverlayItem(flag1Point,this.getResources().getString(R.string.flag1_title),
					this.getResources().getString(R.string.flag1_snippet));
		flag2Point = new GeoPoint(-26290231,28057569);
		flag2 = new OverlayItem(flag2Point,this.getResources().getString(R.string.flag2_title),
					this.getResources().getString(R.string.flag2_snippet));*/
		
		
		// Place flags
		_arenaOverlays = _arenaMapView.getOverlays();
		_myFlagOverlay = new FlagOverlay(_myFlagDrawable,this,_myGameEnvironment);
		_myGameEnvironment.getObjects(this.getResources().getString(R.string.user_gameId), _myFlagOverlay);
		/*myFlagOverlay.addFlag(flag1);
		myFlagOverlay.addFlag(flag2);*/
		_arenaOverlays.add(_myFlagOverlay);
	}
	
	/**
	 * Setup location manager.
	 */
	private final void setupLocationManager() {
		_playerLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
	}
	
	/**
	 * Pause location manager.
	 */
	private final void pauseLocationManager(){
		_playerLocationManager.removeUpdates(this);
	}
	
	/**
	 * Resume location manager.
	 */
	private final void resumeLocationManager() {
		_playerLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
	}
	
	// MapActivity onCreate
	/* (non-Javadoc)
	 * @see com.google.android.maps.MapActivity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState){
		// Call super constructor
		super.onCreate(savedInstanceState);
		setupArena();
		populateArena();
		setupLocationManager();
	}
	
	// MapActivity onPause
	/* (non-Javadoc)
	 * @see com.google.android.maps.MapActivity#onPause()
	 */
	@Override
	protected void onPause(){
		super.onPause();
		pauseLocationManager();
	}
	
	// MapActivity onResume
	/* (non-Javadoc)
	 * @see com.google.android.maps.MapActivity#onResume()
	 */
	@Override
	protected void onResume (){
		super.onResume();
		resumeLocationManager();
	}
	
	// Must be overridden in MapActivity
	/* (non-Javadoc)
	 * @see com.google.android.maps.MapActivity#isRouteDisplayed()
	 */
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	// Actions taken when GPS coordinates change
	/* (non-Javadoc)
	 * @see android.location.LocationListener#onLocationChanged(android.location.Location)
	 */
	@Override
	public void onLocationChanged(Location location) {
		if (location != null) {
			double lattitude = location.getLatitude();
			double longitude = location.getLongitude();
			GeoPoint newLocation = new GeoPoint ((int) (lattitude*1000000),(int) (longitude*1000000));
			_myMapController.animateTo(newLocation);
		}	
	}

	/* (non-Javadoc)
	 * @see android.location.LocationListener#onProviderDisabled(java.lang.String)
	 */
	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see android.location.LocationListener#onProviderEnabled(java.lang.String)
	 */
	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see android.location.LocationListener#onStatusChanged(java.lang.String, int, android.os.Bundle)
	 */
	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}	
}
